<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Xmlrpc_client extends CI_Controller {

	var $orgid;

	function __construct()
	{
		parent::__construct();
		$this->orgid=$this->tank_auth->get_user_id();
	}

	private function handleRequest($method,$paramArray){

		$request = xmlrpc_encode_request($method, $paramArray);
		$context = stream_context_create(array('http' => array(
		    'method' => "POST",
		    'header' => "Content-Type: text/xml\r\nUser-Agent: PHPRPC/1.0\r\n",
		    'content' => $request
		)));

		$server = 'http://localhost/sunshopwebsvc/xmlrpc.php';

		$file = file_get_contents($server, false, $context);

		$response = xmlrpc_decode($file);

		return $response;
	}

	public function get_categories()
	{
		$request=$this->handleRequest("get_categories", $this->orgid);
		echo json_encode($request);
	}

	public function create_category(){
		$name=$this->input->post("name",TRUE);
		$request=$this->handleRequest("create_category", array($this->orgid,$name));
		echo json_encode($request);
	}

	public function update_category(){
		$catid=$this->input->post("catid",TRUE);
		$name=$this->input->post("name",TRUE);
		$request=$this->handleRequest("update_category", array($this->orgid,$catid, $name));
		echo json_encode($request);
	}

	public function delete_category(){
		$catid=$this->input->post("catid",TRUE);
		$request=$this->handleRequest("delete_category", array($this->orgid,$catid));
		echo json_encode($request);
	}


	public function get_all_products(){
		$request=$this->handleRequest("get_all_products", array($this->orgid));
		echo json_encode($request);
	}

	public function create_product(){
		$name=$this->input->post("objname",TRUE);
		$catid=$this->input->post("catid",TRUE);
		$request=$this->handleRequest("create_product", array($this->orgid,$name,$catid));
		echo json_encode($request);
	}

	public function update_product(){
		$objid=$this->input->post("objid",TRUE);
		$name=$this->input->post("name",TRUE);
		$catid=$this->input->post("catid",TRUE);
		$request=$this->handleRequest("update_product", array($this->orgid,$objid, $name,$catid));
		echo json_encode($request);
	}

	public function delete_product(){
		$objid=$this->input->post("objid",TRUE);
		$request=$this->handleRequest("delete_product", array($this->orgid,$objid));
		echo json_encode($request);
	}



	public function get_all_attributes(){

		$objid=$this->input->post("objid",TRUE);

		$request=$this->handleRequest("get_all_prod_attrib", array($this->orgid,$objid));

		echo json_encode($request);
	}

	public function create_attribute(){

		$defaultvalue=$this->input->post("defaultvalue",TRUE);
		$fieldname=$this->input->post("fieldname",TRUE);
		$objid=$this->input->post('objid',TRUE);

		$request=$this->handleRequest("create_prod_attrib", array($this->orgid,$defaultvalue,$fieldname,$objid));

		echo json_encode($request);

	}

	public function update_attribute(){

		$defaultvalue=$this->input->post("defaultvalue",TRUE);
		$fieldname=$this->input->post("fieldname",TRUE);
		$objid=$this->input->post('objid',TRUE);
		$prod_attr_id=$this->input->post('prod_attr_id',TRUE);
		$attr_id=$this->input->post('attrid',TRUE);

		$request=$this->handleRequest("update_prod_attrib", array($this->orgid,$prod_attr_id,$defaultvalue,$fieldname,$objid,$attr_id));

		echo json_encode($request);
	}

	public function delete_attribute(){

		$attrid=$this->input->post("attrid",TRUE);
		$request=$this->handleRequest("delete_prod_attrib", array($this->orgid,$attrid));

		echo json_encode($request);
	}

	public function get_fields(){

		$objid=$this->input->post("objid",TRUE);

		$request=$this->handleRequest("get_fields", array($this->orgid,$objid));

		echo json_encode($request);

	}

	public function create_field(){

		$objid=$this->input->post("objid",TRUE);
		$fieldnum=$this->input->post("fieldnum",TRUE);
		$fieldname=$this->input->post("fieldname",TRUE);
		$datatype=$this->input->post("datatype",TRUE);
		$reference=$this->input->post("reference",TRUE);

		$request=$this->handleRequest("create_field", array($this->orgid,$objid,$fieldnum,$fieldname,$datatype,$reference));

		echo json_encode($request);
	}

	public function update_field(){

		$fieldid=$this->input->post("fieldid",TRUE);
		$fieldnum=$this->input->post("fieldnum",TRUE);
		$fieldname=$this->input->post("fieldname",TRUE);
		$datatype=$this->input->post("datatype",TRUE);
		$reference=$this->input->post("reference",TRUE);

		$request=$this->handleRequest("update_field", array($this->orgid,$fieldid,$fieldnum,$fieldname,$datatype,$reference));

		echo json_encode($request);
	}

	public function delete_field(){

		$fieldid=$this->input->post("fieldid",TRUE);
		$request=$this->handleRequest("delete_field", array($this->orgid,$fieldid));

		echo json_encode($request);
	}


	public function get_all_items(){

		$objid=$this->input->post("objid",TRUE);
		$request=$this->handleRequest("get_all_items", array($this->orgid,$objid));

		echo json_encode($request);
	}

	public function create_item(){

		$objid=$this->input->post("objid",TRUE);

		$data=$this->input->post();
		$xmldata=array();
		foreach(array_keys($data) as $key){
			if(is_int($key)){
				$xmldata[$key]=$data[$key];
			}
				
		}

		echo json_encode($xmldata);
		$request=$this->handleRequest("create_item", array($this->orgid,$objid,json_encode($xmldata)));

		echo json_encode($request);
	}

	public function get_all_items_by_cat(){

		$catid=$this->input->post("catid",TRUE);

		$request=$this->handleRequest("get_items_by_cat", array($this->orgid,$catid));

		echo json_encode($request);
	}

	/* OBJECTS */
	public function get_all_objects(){
		$request=$this->handleRequest("get_all_objects", array($this->orgid));
		if($request==null){
			$request=array();
		}
		echo json_encode($request);
	}

	public function create_object(){
		$name=$this->input->post("name",TRUE);
		$request=$this->handleRequest("create_object", array($this->orgid,$name));
		echo json_encode($request);
	}

	public function update_object(){
		$objid=$this->input->post("objid",TRUE);
		$name=$this->input->post("name",TRUE);
		$request=$this->handleRequest("update_object", array($this->orgid,$objid, $name));
		echo json_encode($request);
	}

	public function delete_object(){
		$objid=$this->input->post("objid",TRUE);
		$request=$this->handleRequest("delete_object", array($this->orgid,$objid));
		echo json_encode($request);
	}

	/* END OBJECTS */

	/* OBJECT ATTRIBUTES  */


	public function get_cust_attr(){

		$objid=$this->input->post("objid",TRUE);

		$request=$this->handleRequest("get_cust_attr", array($this->orgid,$objid));

		echo json_encode($request);
	}

	public function create_cust_attr(){

		$defaultvalue=$this->input->post("defaultvalue",TRUE);
		$fieldname=$this->input->post("fieldname",TRUE);
		$objid=$this->input->post('objid',TRUE);

		$request=$this->handleRequest("create_cust_attr", array($this->orgid,$objid,$fieldname,$defaultvalue));

		echo json_encode($request);

	}

	public function update_cust_attr(){

		$defaultvalue=$this->input->post("defaultvalue",TRUE);
		$fieldname=$this->input->post("fieldname",TRUE);
		$objid=$this->input->post('objid',TRUE);
		$cattrid=$this->input->post('cattrid',TRUE);

		$request=$this->handleRequest("update_cust_attr", array($this->orgid,$cattrid,$fieldname,$defaultvalue));

		echo json_encode($request);
	}

	public function delete_cust_attr(){

		$attrid=$this->input->post("cattrid",TRUE);
		$request=$this->handleRequest("delete_cust_attr", array($this->orgid,$attrid));

		echo json_encode($request);
	}



	/* END OBJECT ATTRIBUTES */

	/* OBJECT ITEMS */

	public function get_all_cust_items(){

		$objid=$this->input->post("objid",TRUE);
		$request=$this->handleRequest("get_all_cust_items", array($this->orgid,$objid));

		echo json_encode($request);
	}

	public function create_cust_item(){

		$objid=$this->input->post("objid",TRUE);
		
		$data=$this->input->post();
		$xmldata=array();
		foreach(array_keys($data) as $key){
			if(is_int($key)){
				$xmldata[$key]=$data[$key];
			}
		
		}
		
		echo json_encode($xmldata);
		$request=$this->handleRequest("create_cust_item", array($this->orgid,$objid,json_encode($xmldata)));
		
		echo json_encode($request);
	}


	public function update_cust_item(){

		$fieldid=$this->input->post("fieldid",TRUE);
		$fieldnum=$this->input->post("fieldnum",TRUE);
		$fieldname=$this->input->post("fieldname",TRUE);
		$datatype=$this->input->post("datatype",TRUE);
		$reference=$this->input->post("reference",TRUE);

		$request=$this->handleRequest("update_field", array($this->orgid,$fieldid,$fieldnum,$fieldname,$datatype,$reference));

		echo json_encode($request);
	}

	public function delete_cust_item(){

		$fieldid=$this->input->post("fieldid",TRUE);
		$request=$this->handleRequest("delete_field", array($this->orgid,$fieldid));

		echo json_encode($request);
	}



	/* END OBJECT ITEMS */
}


